﻿#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

#endregion

namespace Weazel.Toad.Application.Actions
{
  public class SimulateGezelModelAction : ActionBase
  {
    public SimulateGezelModelAction()
    {
      Enabled = false;

      Application.Instance.SelectedItemChanged += 
        new EventHandler<SelectedItemChangedEventArgs>(Instance_SelectedItemChanged);
    }

    public override void Execute(object sender, EventArgs e)
    {      
      if (Application.Instance.SelectedItem is Items.GezelTextFile)
      {
        Items.GezelTextFile gezelFile =
          Application.Instance.SelectedItem as Items.GezelTextFile;

        // copy 'cycles to run'
        gezelFile.Simulation.CyclesToRun = Application.Instance.MainForm.SimulationToolStrip.CyclesToRun;

        gezelFile.Simulation.Run();
      }
    }

    void Instance_SelectedItemChanged(object sender, SelectedItemChangedEventArgs e)
    {
      if (Application.Instance.SelectedItem is Items.GezelTextFile)
      {
        Items.GezelTextFile gezelFile =
          Application.Instance.SelectedItem as Items.GezelTextFile;

        gezelFile.Simulation.SimulationDone += 
          new EventHandler<Items.SimulationEventArgs>(Simulation_SimulationDone);

        gezelFile.Simulation.SimulationStarted += 
          new EventHandler<Items.SimulationEventArgs>(Simulation_SimulationStarted);

        Enabled = gezelFile.Simulation.IsRunning ? false : true;
      }
      else
      {
        Enabled = false;
      }
    }

    void Simulation_SimulationDone(object sender, Weazel.Toad.Application.Items.SimulationEventArgs e)
    {
      Enabled = true;
    }

    void Simulation_SimulationStarted(object sender, Weazel.Toad.Application.Items.SimulationEventArgs e)
    {
      Enabled = false;
    }
  }
}
